# -*- coding: utf-8 -*-

import time
import random
from multiprocessing import (
    Pool,
    Manager,
)
import ctypes

import sys
import logging

logger = logging.getLogger(__name__)


def logger_config():
    logger_format = '%(asctime)s.%(msecs)03d %(processName)24s %(funcName)16s %(lineno)3d  %(message)s'
    logging.basicConfig(format=logger_format, datefmt='%H:%M:%S', level=logging.INFO, stream=sys.stdout)
    pass


def pool_apply(queue, value):
    logger.info('value:%s', value.value)
    if not queue.full():
        queue.put_nowait(value.value)
        pass
    time.sleep(random.random())
    if not queue.empty():
        logger.info('queue:%s', queue.get_nowait(), )
        pass
    pass


def py_manager():
    n = 8
    manager = Manager()
    queue = manager.Queue(maxsize=n)
    value = manager.Value(ctypes.c_char_p, 'hello')

    pool = Pool(processes=n)
    for i in range(n):
        pool.apply(func=pool_apply, args=(queue, value,))
        pass
    pool.close()
    pool.join()
    logger.info('manager')
    pass


if __name__ == '__main__':
    """
    Process Pool配合Manager使用Queue, Value
    """
    logger_config()
    py_manager()
    logger.info('main')
    pass
